﻿@{
    ViewBag.Title = "购物车";
}
@section headerSection{
    <style>
        body{
            font-family:'微软雅黑';
            font-size:14px;
            color:#676a6c;
        }
        a{
            font-size:15px;
        }
    </style>
}

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        
        @if (ViewBag.cart != null)
        {
            <input type="hidden" id="userGuid" value="@ViewBag.cart.userGuid" />
            <input type="hidden" id="ApiUrl" value="@ViewBag.ApiUrl" />
            <h2><span class="fa fa-shopping-cart"></span>&nbsp; @ViewBag.cart.userName 的购物车信息</h2>
            <table class="table">
                <thead>
                    <tr>
                        <th>
                            <input type="checkbox" class="i-checks" id="checkAll">
                        </th>
                        <th>商品名称</th>
                        <th>购买价格</th>
                    </tr>
                </thead>
                <tbody>
                    @for (var i = 0; i < ViewBag.cart.commodityList.Count; i++)
                    {
                        <tr>
                            <td>
                                <input type="checkbox" class="i-checks checkCommodity" money="@ViewBag.cart.commodityList[i].price" commId="@ViewBag.cart.commodityList[i].id">
                            </td>
                            <td><a href="#">@ViewBag.cart.commodityList[i].name</a></td>
                            <td style="color:red;">￥@ViewBag.cart.commodityList[i].price</td>
                        </tr>
                    }
                </tbody>
            </table>

            <p style="text-align:right;"><span style="font-size:150%;">一共 @ViewBag.cart.totalCommodity 件商品，</span><span style="color:red;font-size:200%;font-weight: bold;padding-right:30px;">应付总额：@ViewBag.cart.totalPrice 元</span></p>
            <p style="text-align:right;"><span style="font-size:130%;" id="checkCommodityCount">选中0件商品，</span><span id="checkCommodityMoney" style="color:#f7a54a;font-size:190%;font-weight: bold;padding-right:30px;">应付总额：0 元</span></p>
            <p style="text-align:right;">
                <button id="createOrder" type="button" style="margin-right:30px;" class="btn btn-w-m btn-warning">
                    <i class="fa fa-bars"></i>下单生成未付款订单</button>
             </p>
            }
            else
            {
            <h1><span class="fa fa-shopping-cart"></span>&nbsp;购物车</h1>
            }
    </div>
</div>
<script>
    $(function () {
        var ApiUrl = $('#ApiUrl').val();
        var userId = $('#userGuid').val();

        //选中总金额、选中数量
        var checkCommodityMoney = 0;
        var checkCommodityCount = 0;
        
        //更新选中金额和数量的展示信息
        var updateCheckeCommodityInfo = function () {
            $('#checkCommodityCount').text('选中' + checkCommodityCount + '件商品，');
            $('#checkCommodityMoney').text('应付总额：' + checkCommodityMoney + ' 元');
        }

        $('.checkCommodity').on('ifChecked', function (event) {
            //选中商品
            $(this).attr('sed', 1);
            checkCommodityMoney = accAdd(checkCommodityMoney, $(this).attr('money'));
            checkCommodityCount++;
            updateCheckeCommodityInfo();
        });

        $('.checkCommodity').on('ifUnchecked', function (event) {
            //取消选中商品

            $(this).attr('sed',0);
            checkCommodityMoney = accSub(checkCommodityMoney, $(this).attr('money'));
            checkCommodityCount--;
            updateCheckeCommodityInfo();
        });

        //全选
        $('#checkAll').on('ifChecked', function (event) {
            //全选，勾选
            $('.checkCommodity').iCheck('check');
        });
        $('#checkAll').on('ifUnchecked', function (event) {
            //全选，取消
            $('.checkCommodity').iCheck('uncheck');
        });

        //初始化插件
        $(".i-checks").iCheck({
            checkboxClass: "icheckbox_square-green",
            radioClass: "iradio_square-green"
        });

        //获取所有选中商品的ID
        var getCheckCommodityIdArray = function () {
            var checkCommodityIdArray = [];

            $(".checkCommodity").each(function () {
                console.log($(this).attr('sed'));
                if ($(this).attr("sed") == 1) {//这里就可以判断当前是否被选择了 
                    var cmid = $(this).attr('commId');
                    checkCommodityIdArray.push(parseInt(cmid));
                }
            });
            return checkCommodityIdArray;
        }

        //下单按钮
        $('#createOrder').on('click', function () {
            var checkCommIdArray = getCheckCommodityIdArray();
            if (checkCommIdArray.length == 0) {
                window.parent.layer.msg('请选择商品再下单', { icon: 5 });
                return;
            }

            var loadIndex = window.parent.layer.msg('正在下单...', { icon: 16, shade: [0.3, '#393D49'], time: 0 });
            var orderUrl = ApiUrl + 'api/ShoppingCartAPI/PlaceAnOrder';
            var requestData = { userid: userId, CommodityId: checkCommIdArray };
            $.ajax({
                url: orderUrl,
                type:'post',
                data: JSON.stringify(requestData),
                contentType: "application/json",
                dataType:'json',
                success: function (server) {
                    window.parent.layer.close(loadIndex);
                    if (server.errcode == 0) {
                        window.parent.layer.msg('下单成功，在未付款订单中', { icon: 1 });
                        window.location.reload();
                    } else {
                        window.parent.layer.alert('下单失败：' + server.errmsg, { icon: 7 });
                    }
                },
                error: function (r) {
                    window.parent.layer.close(loadIndex);
                    window.parent.layer.alert('下单失败，服务器错误', { icon: 5 });
                }
            });
        });
    });

    //金额相加
    function accAdd(arg1,arg2){
        var r1,r2,m;
        try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
        try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
        m=Math.pow(10,Math.max(r1,r2))
        return (arg1*m+arg2*m)/m
    }
    //金额相减
    function accSub(arg1, arg2) {
        return accAdd(arg1, -arg2);
    }
    
</script>
